<template>
  <div class="project-logs">
    <div class="logs-header">
      <h3>{{ deployment?.projectName }} - 部署日志</h3>
      <div class="logs-actions">
        <el-button size="small" @click="refreshLogs" :loading="loading">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
        <el-select v-model="logLimit" size="small" @change="loadLogs">
          <el-option label="最近50条" :value="50" />
          <el-option label="最近100条" :value="100" />
          <el-option label="最近200条" :value="200" />
        </el-select>
      </div>
    </div>
    
    <div class="logs-content">
      <el-empty v-if="logs.length === 0" description="暂无日志记录" />
      <div v-else class="log-list">
        <div
          v-for="log in logs"
          :key="log.id"
          class="log-item"
          :class="`log-${log.type}`"
        >
          <div class="log-header">
            <span class="log-time">{{ formatTime(log.timestamp) }}</span>
            <el-tag :type="getLogType(log.type)" size="small">
              {{ getLogTypeText(log.type) }}
            </el-tag>
          </div>
          <div class="log-message">{{ log.message }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { useDeploymentStore } from '@/stores/deployment'

interface Props {
  deployment: any
}

const props = defineProps<Props>()
const deploymentStore = useDeploymentStore()

const logs = ref<any[]>([])
const loading = ref(false)
const logLimit = ref(50)

const loadLogs = async () => {
  if (!props.deployment?.id) return
  
  try {
    loading.value = true
    const projectLogs = await deploymentStore.getProjectLogs(props.deployment.id, logLimit.value)
    logs.value = projectLogs
  } catch (error) {
    console.error('加载项目日志失败:', error)
  } finally {
    loading.value = false
  }
}

const refreshLogs = () => {
  loadLogs()
}

const formatTime = (time: string) => {
  return new Date(time).toLocaleString('zh-CN')
}

const getLogType = (type: string) => {
  const typeMap: Record<string, string> = {
    info: 'info',
    error: 'danger',
    warning: 'warning'
  }
  return typeMap[type] || 'info'
}

const getLogTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    info: '信息',
    error: '错误',
    warning: '警告'
  }
  return typeMap[type] || '信息'
}

onMounted(() => {
  loadLogs()
})

watch(() => props.deployment?.id, () => {
  if (props.deployment?.id) {
    loadLogs()
  }
})
</script>

<style scoped>
.project-logs {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.logs-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e4e7ed;
  background-color: #fafafa;
}

.logs-header h3 {
  margin: 0;
  color: #303133;
}

.logs-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.logs-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.log-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.log-item {
  padding: 12px;
  border-radius: 6px;
  border-left: 4px solid #e4e7ed;
  background-color: #fafafa;
}

.log-item.log-info {
  border-left-color: #409eff;
  background-color: #f0f9ff;
}

.log-item.log-error {
  border-left-color: #f56c6c;
  background-color: #fef0f0;
}

.log-item.log-warning {
  border-left-color: #e6a23c;
  background-color: #fdf6ec;
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.log-time {
  font-size: 12px;
  color: #909399;
}

.log-message {
  font-size: 14px;
  color: #303133;
  line-height: 1.5;
  word-break: break-word;
}
</style>
